package com.ycsj.goldmedalnewconcept.parent.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.image.ImageUtil;
import com.ycsj.common.manager.PickChooseManager;
import com.ycsj.common.view.vp.SuperViewPager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.parent.bean.ScoreInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 分数详情界面
 * Created by Administrator on 2018/6/20 0020.
 */

public class ParentsScoreDetailsActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivBack;
    /**
     * 学生名称
     */
    private TextView tvStTitle;
    /**
     * 测试分数:98.0
     */
    private TextView tvScore;
    private SuperViewPager vp;
    private Button btFillReceipt;

    //老师评语
    private TextView tvContent;

    //家长回执
    private LinearLayout ll_pa_content;

    private TextView tv_pa_content;

    //页面数据对象
    private ScoreInfo.DataBean info;
    private List<String> homeWorkUrls;


    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_score_details;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化视图
        initView();

        //获取页面对象
        initIntent();

    }


    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        tvStTitle = findViewById(R.id.tv_title);
        tvScore = findViewById(R.id.tv_score);
        vp = findViewById(R.id.vp);
        btFillReceipt = findViewById(R.id.bt_fill_receipt);
        btFillReceipt.setOnClickListener(this);
        tvContent = findViewById(R.id.tv_content);


        ll_pa_content = findViewById(R.id.ll_pa_content);
        tv_pa_content = findViewById(R.id.tv_pa_content);
    }

    /**
     * 获取页面对象数据
     */
    private void initIntent() {
        info = (ScoreInfo.DataBean) getIntent().getSerializableExtra("data");

        if (null != info) {

            if (!TextUtils.isEmpty(info.getPARENT_CONTENT())) {
                //已填写回执
                btFillReceipt.setVisibility(View.GONE);
                ll_pa_content.setVisibility(View.VISIBLE);
                tv_pa_content.setText(info.getPARENT_CONTENT());
            }

            //设置考试名称
            tvStTitle.setText(info.getTAG() + "");
            //设置分数
            tvScore.setText("测试分数:" + info.getSCORE());
            //老师评语
            if (TextUtils.isEmpty(info.getCONTENT())) {
                //无评语
                tvContent.setText("暂无评语");
            } else {
                tvContent.setText(info.getCONTENT());
            }

            //处理试卷图片
            //获取试卷图片
            homeWorkUrls = new ArrayList<>();
            if (!TextUtils.isEmpty(info.getPICTURE1())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE1());
            }
            if (!TextUtils.isEmpty(info.getPICTURE2())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE2());
            }

            if (!TextUtils.isEmpty(info.getPICTURE3())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE3());
            }

            if (!TextUtils.isEmpty(info.getPICTURE4())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE4());
            }

            if (!TextUtils.isEmpty(info.getPICTURE5())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE5());
            }
            if (!TextUtils.isEmpty(info.getPICTURE6())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE6());
            }
            if (!TextUtils.isEmpty(info.getPICTURE7())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE7());
            }
            if (!TextUtils.isEmpty(info.getPICTURE8())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE8());
            }
            if (!TextUtils.isEmpty(info.getPICTURE9())) {
                homeWorkUrls.add(info.getPIC_URL() + info.getPICTURE9());
            }

            if (null == homeWorkUrls || homeWorkUrls.size() == 0) {
                //证明没有试卷图片
                vp.setVisibility(View.INVISIBLE);
            } else {
                vp.setVisibility(View.VISIBLE);
                //设置Vp
                initVp();
            }
        }

    }

    /**
     * 设置ViewPager的数据
     */
    private void initVp() {
        VpAdapter adapter = new VpAdapter();
        vp.setAdapter(adapter);
        vp.setOffscreenPageLimit(homeWorkUrls.size());
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.bt_fill_receipt) {
            //填写回执
            Intent intent = new Intent(this, ParentsFillReceiptActivity.class);
            intent.putExtra("rowid", info.getROW_ID() + "");
            startActivity(intent);
            finish();
        }
    }


    /**
     * ViewPager的适配器
     */
    private class VpAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return homeWorkUrls == null ? 0 : homeWorkUrls.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (object instanceof RoundedImageView) {
                RoundedImageView scaleView = (RoundedImageView) object;
                container.removeView(scaleView);
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            RoundedImageView roundedImageView = new RoundedImageView(ParentsScoreDetailsActivity.this);
            roundedImageView.setCornerRadius(12.0f);
            roundedImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ImageUtil.displayImage(ParentsScoreDetailsActivity.this, roundedImageView, homeWorkUrls.get(position), R.drawable.head_default);
            //点击查看大图
            roundedImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    PickChooseManager.startShowBigActivity(ParentsScoreDetailsActivity.this, homeWorkUrls, position);
                }
            });
            container.addView(roundedImageView);
            return roundedImageView;
        }
    }
}
